/*******************************************************************
* Copyright (c) 2010 BSUIR <romkisel@gmail.com>

* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:

* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
********************************************************************/

#include "mainwindow.h"
#include <QtGui>
#include <QDialog>
#include <QAction>

MainWindow::MainWindow(OptixScene *scene, QMainWindow *prnt)
    : QMainWindow(prnt)
{
    this->setupUi(this);            //setup main form
    this->view->setScene(scene);    //connect scene to the view
    this->scene = scene;

//===========================================================================
//connect slots
    connect(srlRotate, SIGNAL(valueChanged(int)), scene, SLOT(srlRotateChange(int)));
    connect(srlRotate, SIGNAL(valueChanged(int)), spinRotate, SLOT(setValue(int)));
    connect(spinRotate, SIGNAL(valueChanged(int)), srlRotate, SLOT(setValue(int)));
    connect(srlScale, SIGNAL(valueChanged(int)), scene, SLOT(srlScaleChange(int)));
    connect(srlScale, SIGNAL(valueChanged(int)), spinScale, SLOT(setValue(int)));
    connect(spinScale, SIGNAL(valueChanged(int)), srlScale, SLOT(setValue(int)));
    connect(srlScaleX, SIGNAL(valueChanged(int)), scene, SLOT(srlScaleXChange(int)));
    connect(srlScaleX, SIGNAL(valueChanged(int)), spinScaleX, SLOT(setValue(int)));
    connect(spinScaleX, SIGNAL(valueChanged(int)), srlScaleX, SLOT(setValue(int)));
    connect(srlScaleY, SIGNAL(valueChanged(int)), scene, SLOT(srlScaleYChange(int)));
    connect(srlScaleY, SIGNAL(valueChanged(int)), spinScaleY, SLOT(setValue(int)));
    connect(spinScaleY, SIGNAL(valueChanged(int)), srlScaleY, SLOT(setValue(int)));
//===========================================================================
    connect(scene, SIGNAL(changed(const QList<QRectF> &)), scene, SLOT(changeDraw(const QList<QRectF> &)));
    connect(scene, SIGNAL(selectionChanged()), scene, SLOT(selectItem()));
    connect(scene, SIGNAL(selectionChanged()), this, SLOT(changeSelection()));
//===========================================================================
    connect(action_New, SIGNAL(triggered()), scene, SLOT(actionNew()));
    connect(action_Open, SIGNAL(triggered()), scene, SLOT(actionOpen()));
    connect(action_Save, SIGNAL(triggered()), scene, SLOT(actionSave()));
    connect(action_saveAs, SIGNAL(triggered()), scene, SLOT(actionSaveAs()));
    connect(action_delete, SIGNAL(triggered()), scene, SLOT(deleteItems()));
    connect(action_rotate, SIGNAL(triggered()), scene, SLOT(actionRotate()));
    connect(action_scale, SIGNAL(triggered()), scene, SLOT(actionScale()));
    connect(action_toFront, SIGNAL(triggered()), scene, SLOT(actionToFront()));
    connect(action_toBack, SIGNAL(triggered()), scene, SLOT(actionToBack()));
    connect(action_color, SIGNAL(triggered()), scene, SLOT(actionColor()));
    connect(action_settings, SIGNAL(triggered()), scene, SLOT(actionSettings()));
    connect(action_about, SIGNAL(triggered()), scene, SLOT(actionAbout()));
    connect(action_Exit, SIGNAL(triggered()), this, SLOT(close()));
//===========================================================================
    scene->setContextMenu(this->menu_element);
    scene->setToolBar(this->toolBar);
}

void MainWindow::changeSelection()
{
    if (scene->selectedItems().size() == 1)
    {
        QGraphicsItem *item = scene->selectedItems().first();

        //FIXME: item->rotation() doesn't work!
        OptixItem *oi;
        if (oi = dynamic_cast<OptixItem *>(item))
        {
            srlRotate->setValue(oi->getAngle());

            srlScaleX->setValue((int)(oi->getScX()*50));
            srlScaleY->setValue((int)(oi->getScY()*50));
        }
    }
}
